package official_account

import (
	Result "code.gitea.io/gitea/routers/entity"
	wechat_official_account_service "code.gitea.io/gitea/services/wechat/official_account"
	"net/http"
)

// QrCheckCodeStatus 检查二维码扫描状态
/**
  - 微信服务器验证消息
  - GET /api/wechat/official-account/login/qr/check-status
  - 请求参数：
  - ticket: 微信公众号带参数临时二维码 ticket
  - _:      UNIX时间戳，仅用作防止GET请求被缓存，保证每次GET 请求都能够到达服务器
  - 响应参数：（请使用VAR定义）
  - {
    Code: ,  // 状态码，只有在 HTTP 200 OK 后，该字段才有意义
    Msg: ,   //
    Data:
    }
*/
func QrCheckCodeStatus(responseWriter http.ResponseWriter, request *http.Request) {

	// 设置响应头为 JSON 格式
	responseWriter.Header().Set("Content-Type", "application/json")

	// 从请求中提取 ticket 参数
	ticket := request.URL.Query().Get("ticket")
	if ticket == "" {
		Result.RespFailedIllegalWechatQrTicket.RespondJson2HttpResponseWriter(responseWriter)
		return
	}

	// 调用 WeChat Official Account Service 层，获取二维码扫码状态
	qrStatusVO, err := wechat_official_account_service.GetTempQRStatus(ticket)
	if err != nil {
		respFailed := Result.ResultType{
			Code: Result.RespFailedGetWechatOfficialAccountTempQRStatus.Code,
			Msg:  Result.RespFailedGetWechatOfficialAccountTempQRStatus.Msg,
			Data: map[string]string{
				"ErrorMsg": err.Error(),
			},
		}
		respFailed.RespondJson2HttpResponseWriter(responseWriter)
		return
	}

	// 将扫码信息返回
	result := Result.ResultType{
		Code: Result.RespSuccess.Code,
		Msg:  Result.RespSuccess.Msg,
		Data: qrStatusVO,
	}
	result.RespondJson2HttpResponseWriter(responseWriter)
}
